<?php

require('config.php');
$page = new rootLayoutPage();

$mapDistrict = modelMap::getNewsByUrl('map.php?type=2');
$balise_title = $mapDistrict->title;
$description = $mapDistrict->meta_title;
$page->importJavaScript('http://maps.google.com/maps?file=api&amp;v=2&amp;key='.GOOGLE_MAP_KEY);
$page->importJavaScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0');
$page->importJavaScript('http://www.google.com/uds/solutions/localsearch/gmlocalsearch.js');
$page->importStyleSheet('http://www.google.com/uds/css/gsearch.css');
$page->importStyleSheet('http://www.google.com/uds/solutions/localsearch/gmlocalsearch.css');
$map_xml = tmxTranslate('map.xml', REP_TPL_XLAT);
if(isset($_GET['type'])) {
	if($_GET['type']>0) {
		if(!modelGoogleMap::existGoogleMapByIDGategory($_GET['type'])) redirect(LANGUAGE_FOLDER."/map");
	}
}
	if(isset($_GET['type'])) {
		//$newstByTypeLink = modelNews::getNewsByUrl('map.php?type='.$_GET['type']);
		$newsByTypeLink = modelNews::getExistInCategory('map.php?type='.$_GET['type']);
		//if(!$newsByTypeLink) redirectFront(LANGUAGE_FOLDER."/index");
		$title = $newsByTypeLink->title;
		$id_menu_left_tree = modelNews::getNewsMenuleft($newsByTypeLink->language_link);
		$id_in_tree = modelMap::getIDInTree($newsByTypeLink->language_link);

	}else{
		$newsByTypeLink = modelNews::getNewsByUrl('map.php');
		//if(!$newsByTypeLink) redirectFront(LANGUAGE_FOLDER."/index");
		$title = $newsByTypeLink->title;
		$id_menu_left_tree = modelNews::getNewsMenuleft($newsByTypeLink->language_link);
		$id_in_tree = modelMap::getIDInTree($newsByTypeLink->language_link);
	}

	if($id_in_tree != null and $id_in_tree > 0){
	if($id_menu_left_tree->need_login == STATUS_NEED_CONNECTION){
		if(!isset($_SESSION['id_user'])){
			$_SESSION['referer_connexion'] =  "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
			redirectFront('connexion');
		}
	}
	}else redirectFront('connexion');
	//--------Menu Page ---------------------------//
		$menu = new layoutMenuLeft();
		$menuPage = new layoutMenuPage();
		$menuPage->setTable('zz_data_url');
		$menuPage->setIdShow($id_menu_left_tree->id_menu_left_tree);
		$menu->pushMenu($menuPage);
		$page->setMenuPage($menu);
	//--------Menu indicator--------------//
		/*$menuIndictor = new layoutMenuIndicator();
		$menuIndictor->idTree($id_menu_left_tree->id_menu_left_tree);
		$menuIndictor->title($title);
		$page->setMenuIndicator($menuIndictor);*/
	//--------Menu Header-----------------//
		$header = new layoutHeader();
		$header->setArrayUrl($arr);
		$header->setIdShow($id_menu_left_tree->id_menu_left_tree);
		$header->setIdOutTree($id_in_tree);
		$page->setHeader($header);

//**********************//
$page->setPageSameLevel($id_menu_left_tree->id_menu_left_tree);
$page->setBaliseTitle($balise_title);
$page->setDescription($description);
$extension = filename_extension($_SERVER['REQUEST_URI']);
if($extension == 'html')$page->setCanonical('');

$tpl_file='map.tpl';
$map = new flyLayout(REP_TPL . $tpl_file);
$map->start();

$map->setVariable('language_folder', LANGUAGE_FOLDER);
if($id_menu_left_tree->id_menu_left_tree){
    	$listPage = modelMenuLeft::completePathFromRoot($id_menu_left_tree->id_menu_left_tree);
		foreach($listPage as $key => $pageAriane) {

			if($listPage->index() == 0) continue; // pour sauter la racine

			if($listPage->size() == $key+1) break; //ignor current page

			if($listPage->index() == 0)$map->hideBlock('bar1');else $map->showBlock('bar1');
			if($key<$listPage->size()-2) $map->showBlock('bar');
			if($key == $listPage->size()-2)$map->hideBlock('bar');


			$data = modelMenuLeft::getDataFromContentPage($pageAriane->table_name,$pageAriane->language_link,$pageAriane->language_code);
				if($data){
					$map->setVariable('nom_page', $data->title);
					$url_page = $data->url_rewriting.'-'.$data->language_link.'.html';
					$map->setVariable('url_page', $data->url?$data->url:$url_page);
					$map->setVariable('title_page', $data->meta_title);
				}
			$map->parseList('list_page');

		}
		$map->setVariable('page_current', affiche($title,'sort_title_news_current'));
		$map->hideBlock('show_indicator');$map->showBlock('indicator');
}else{$map->showBlock('show_indicator');$map->hideBlock('indicator');}

if(isset($_GET['type'])) {
	if($_GET['type']>0) {
		$listGoogleMap = modelGoogleMap::getListGoogleMap($_GET['type']);
		$seper=0;
		if($listGoogleMap->size()>5) {
			for($i=1;$i<$listGoogleMap->size()/2;$i++){
				$all = $listGoogleMap->size();
				if($all-($i+$i)<=$i){ $seper = $i; break;}

			}
		}
		if($listGoogleMap->size()>0) {
			$format = modelUploadFormat::getObject(FORMAT_PREVIEW_MAP);
			foreach($listGoogleMap as $index => $google_map) {
				$indexOrder = $index + 1;
				if($seper==0){
					$map->setVariable('index1', $indexOrder);
					$map->setVariable('name_place1', $google_map->name_place);
					$map->setVariable('language_link1', $google_map->language_link);
					$map->setVariable('title1', $google_map->name_place);
					$map->parseList('listGoogleMap1');
				}else{
					if($index<$seper){
						$map->setVariable('index1', $indexOrder);
						$map->setVariable('name_place1', $google_map->name_place);
						$map->setVariable('language_link1', $google_map->language_link);
						$map->setVariable('title1', $google_map->name_place);
						$map->parseList('listGoogleMap1');
					}else if($index>=$seper and $index<$seper+$seper){
						$map->setVariable('index2', $indexOrder);
						$map->setVariable('name_place2', $google_map->name_place);
						$map->setVariable('language_link2', $google_map->language_link);
						$map->setVariable('title2', $google_map->name_place);
						$map->parseList('listGoogleMap2');
					}else if($index>=$seper+$seper){
						$map->setVariable('index3', $indexOrder);
						$map->setVariable('name_place3', $google_map->name_place);
						$map->setVariable('language_link3', $google_map->language_link);
						$map->setVariable('title3', $google_map->name_place);
						$map->parseList('listGoogleMap3');
					}
				}

				$image = modelUploadFile::getObject($google_map->id_image);
				if($image){
					if(file_exists($image->url_repertoire.$format->url_format.$image->nom_serveur)){
						$url_image = "<img src='/". $image->url_repertoire.$format->url_format.$image->nom_serveur. "' />";
						$url_image = "<td width='40'>".$url_image."</td>";
						$map->setVariable('url_image',$url_image );
					}else{
						$map->setVariable('url_image', '');
					}
				}else{
					$map->setVariable('url_image', '');
				}
				// parse list google map for javascript
				$map->setVariable('index', $indexOrder);
				$map->setVariable('name_place', htmlspecialchars($google_map->name_place));
				//$map->setVariable('chief', htmlspecialchars($google_map->chief!=''?$map_xml['text_chief'].' '.$google_map->chief:''));
				$map->setVariable('address', htmlspecialchars($google_map->address));
				//$map->setVariable('phone_number', htmlspecialchars($google_map->phone_number!=''?$map_xml['text_telephone'].$google_map->phone_number:''));
				$map->setVariable('email', htmlspecialchars($google_map->email!=''?$map_xml['text_email'].$google_map->email:''));
				$content = trim(preg_replace('/"/', "'", $google_map->content));
				if($google_map->content !=''){
	    			$map->setVariable('link_detail', $content);
	    		}else{
	    			$map->setVariable('link_detail', '');
	    		}

				if($google_map->website !=''){
	    			$web_site = $google_map->website ;
		    		$http = substr($web_site,0,4);
		    		$map->setVariable('website', $http=='http'?$google_map->website:'http://'.$google_map->website);
		    		$map->setVariable('text_website', $map_xml['text_website']);
		    		$map->showBlock('block_website');
	    		}else{
	    			$map->setVariable('website', '');
	    			$map->setVariable('text_website', '');
	    			$map->hideBlock('block_website');
	    		}
				/*if($google_map->chief !=''){
					$map->showBlock('block_chief');
				}else{
					$map->hideBlock('block_chief');
				}*/
				if($google_map->email !=''){
					$map->showBlock('block_email');
				}else{
					$map->hideBlock('block_email');
				}
				/*if($google_map->phone_number !=''){
					$map->showBlock('block_phone');
				}else{
					$map->hideBlock('block_phone');
				}*/
				$map->setVariable('latitude', $google_map->latitude);
				$map->setVariable('longitude', $google_map->longitude);
				$map->parseList('listGoogleMapJavascript_html');
				$map->parseList('listGoogleMapJavascript');

			}
		}
	}
}

$listCategory = modelGoogleMap::getListCategory();
$mapCenter = modelGoogleMap::getDataCenter(modelGoogleMap::ID_MAP_FRENCH);
$map->setVariable('latitude_map', $mapCenter->latitude_map);
$map->setVariable('longitude_map',$mapCenter->longitude_map);
$map->setVariable('zome_level', $mapCenter->zome_level);
if($listCategory->size()>0) {
	foreach($listCategory as $key => $category) {
		$selected = (isset($_GET['type']) && $_GET['type']==$category->language_link)?'selected="selected"':'';
		$map->setVariable('selected', $selected);
		$map->setVariable('language_link', $category->language_link);
		$map->setVariable('title', $category->title);
		$map->parseList('listCategory');
	}
}

$map->stop();

$page->setBody($map);
$page->display();

?>